package com.ff.kvm.common.third_util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.region.Region;

import java.io.FileInputStream;
import java.io.IOException;

public class TencentOSSUtil {
    private static final String bucketName = "kvm-1256144218";
    private static final String region = "ap-chengdu";
    private static final String secretId = "AKIDpX7Xln3bIpBMpAhLCybTCoTxOhpmmAtb";
    private static final String secretKey = "sbBaovMo3DGVag3LfrtkR3BYt35whHsR";
    private static final String url = "http://kvm-1256144218.cos.ap-chengdu.myqcloud.com/";

    public static String upload(FileInputStream fileInputStream, String name)
            throws CosClientException, IOException
    {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);

        ClientConfig clientConfig = new ClientConfig(new Region(region));

        COSClient cosClient = new COSClient(cred, clientConfig);

        ObjectMetadata objectMetadata = new ObjectMetadata();

        objectMetadata.setContentLength(fileInputStream.available());

        objectMetadata.setContentType("image/jpeg");
        cosClient.putObject(bucketName, name, fileInputStream, objectMetadata);
        return url + name;
    }
}
